//
//  YWWriteRuntimeCell.m
//  maintain
//
//  Created by 烟雨云渊 on 16/8/9.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWWriteRuntimeCell.h"
#import "Masonry.h"
#import "YWMacro.h"
#import "YWEquipmentType.h"
#import "UIView+Extension.h"

@interface YWWriteRuntimeCell()<UITextFieldDelegate>
/*  运行参数名称  */
@property (strong, nonatomic) UILabel *parameterNameLabel;
/*  填写参数值的textfield  */
@property (strong, nonatomic) UITextField *parameterTextF;
@end

@implementation YWWriteRuntimeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.textLabel.font = kContentFont;
        [self setUpSubviews];
    }
    
    return self;
}

-(void)setRunTimeParameter:(YWRunTimeParameter *)runTimeParameter{
    _runTimeParameter = runTimeParameter;
    if (runTimeParameter && runTimeParameter.fieldName.length>0) {
        _parameterNameLabel.text = _runTimeParameter.fieldName;
        _parameterTextF.hidden = NO;
        _parameterNameLabel.hidden = NO;
        self.textLabel.hidden = YES;
    }else{
        self.textLabel.text = @"无参数";
        _parameterNameLabel.hidden = YES;
        _parameterTextF.hidden = YES;
        self.textLabel.hidden = NO;
    }
    if (runTimeParameter) {
        NSString *isRequiredStr = @"参考区间";
        NSString *placeHolder = @"请输入";
        if (runTimeParameter.required) {
            isRequiredStr = @"* 参考区间";
            placeHolder = @"* 请输入";
        }
        if (runTimeParameter.minFieldValue || runTimeParameter.maxFieldValue) {
            placeHolder = [NSString stringWithFormat:@"%@: %ld ~ %ld",isRequiredStr,(long)runTimeParameter.minFieldValue,(long)runTimeParameter.maxFieldValue];
        }
        _parameterTextF.placeholder = placeHolder;
        _parameterTextF.keyboardType = UIKeyboardTypeDefault;
        if (runTimeParameter.fieldType.fieldTypeId == YWEquipmentFieldTypeIdNumber) {
            _parameterTextF.keyboardType = UIKeyboardTypeDecimalPad;
        }
        if (runTimeParameter.fieldValue.length) {
            _parameterTextF.text = runTimeParameter.fieldValue;
        }else{
            _parameterTextF.text = nil;
        }
    }
}

- (void)setUpSubviews{
    UIView *superView = self.contentView;
    [self.parameterNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).offset(10);
        make.left.mas_equalTo(superView.mas_left).offset(10);
        make.right.mas_equalTo(superView.mas_right).offset(-10);
        make.bottom.mas_equalTo(self.parameterTextF.mas_top).offset(-5);
    }];
    
    [self.parameterTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superView.mas_left).offset(10);
        make.right.mas_equalTo(superView.mas_right).offset(-10);
//        make.bottom.mas_equalTo(superView.mas_bottom).offset(-10);
        make.height.mas_equalTo(30);
    }];
}

- (UILabel *)parameterNameLabel{
    if (!_parameterNameLabel) {
        UILabel * label = [[UILabel alloc] init];
        label.font = kContentFont;
        [self.contentView addSubview:label];
        _parameterNameLabel = label;
    }
    return _parameterNameLabel;
}

- (UITextField *)parameterTextF{
    if (!_parameterTextF) {
        UITextField * textField = [[UITextField alloc] init];
        textField.font = kContentFont;
        textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
        textField.layer.borderWidth = 1.0;
        textField.clipsToBounds = YES;
        textField.layer.cornerRadius = 4.0;
        textField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 1)];
        textField.leftViewMode = UITextFieldViewModeAlways;
        textField.delegate = self;
        [self.contentView addSubview:textField];
        _parameterTextF = textField;
    }
    return _parameterTextF;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    NSString *runTimeValue = [self.parameterTextF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    _runTimeParameter.fieldValue = runTimeValue;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.textLabel.x = kMarginW;
}

@end
